<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace files\images;

defined('ANS') or die();

abstract class Image {
	public $settings = array();

	protected $Debug;


	/**
	 * public function __construct ()
	 */
	function __construct () {
		global $Debug;

		$this->Debug = $Debug;
	}


	/**
	 * public function setSettings (array $settings = null)
	 *
	 * return boolean
	 */
	public function setSettings ($settings = null) {
		if (is_null($settings)) {
			$settings = 'images';
		}

		if (is_string($settings) && $Config->config[$settings]) {
			global $Config;
			$this->settings = $Config->config[$settings];
			return true;
		}

		if (is_array($settings)) {
			$this->settings = $settings;
			return true;
		}

		return false;
	}


	/**
	 * public function transform ([string $operations])
	 *
	 * Execute a list of operations
	 */
	public function transform ($operations = '') {
		if (!$operations) {
			return $this;
		}

		$array_operations = $this->getOperations($operations);

		foreach ($array_operations as $operation) {
			$function = $operation['function'];
			$params = $operation['params'];

			switch ($function) {
				case 'flip':
				case 'flop':
					$this->$function();
					break;

				case 'convert':
				case 'alpha':
					$this->$function($params[0]);
					break;

				case 'zoomCrop':
				case 'rotate':
					$this->$function($params[0], $params[1]);
					break;

				case 'merge':
				case 'resize':
					$this->$function($params[0], $params[1], $params[2]);
					break;

				case 'crop':
					$this->$function($params[0], $params[1], $params[2], $params[3]);
					break;
				default:
					$this->Debug->error('image', __('No valid operation (%s) for image transform. All operation string is %s', array($function, implode(',', $params))));
			}
		}

		if ($this->settings['cache']) {
			global $Config;

			$cache_folder = $Config->phpcan_paths['cache'].'images/';

			if (!is_dir($cache_folder) && !@mkdir($cache_folder, 0755)) {
				$this->Debug->error('file', 'There was an error creating the folder "%s"', $cache_folder);
			}

			if (is_writable($cache_folder)) {
				$this->save($cache_folder.alphaNumeric($this->info['file'].$operations));
			}
		}

		return $this;
	}

	/**
	 * private function getOperations (array $operations)
	 *
	 * Split string operations and convert it to array
	 *
	 * return array
	 */
	private function getOperations ($operations) {
		$return = array();
		$array = explode('|', $operations);

		foreach ($array as $each) {
			$params = explode(',', $each);

			while (empty($params[0]) && (count($params) > 0)) {
				array_shift($params);
			}

			$return[] = array(
				'function' => array_shift($params),
				'params' => $params
			);
		}

		return $return;
	}

	/**
	 * public function get ([string $image])
	 *
	 * return the image object
	 */
	public function get ($image = '') {
		if ($image) {
			if (!$this->load($image)) {
				return false;
			}
		}

		return $this->image;
	}


	/**
	 * public function set ([object $image])
	 *
	 * Add an image to the class
	 */
	public function set ($image) {
		$this->image = $image;

		return $this;
	}


	/**
	 * protected function position (int/string $position, int $size, int $canvas)
	 *
	 * Return integer
	 */
	protected function position ($position, $size, $canvas) {
		if (is_int($position)) {
			return $position;
		}

		switch ($position) {
			case 'top':
			case 'left':
				$position = 0;
				break;

			case 'middle':
			case 'center':
				$position = ($canvas/2) - ($size/2);
				break;

			case 'right':
			case 'bottom':
				$position = $canvas - $size;
				break;

			default:
				$position = 0;
		}

		return $position;
	}


	/**
	 * protected function enlarge ($width, $height, $image_width, $image_height)
	 *
	 * Return boolean
	 */
	protected function enlarge ($width, $height, $image_width, $image_height) {
		$w = $h = false;

		if ($width && $width > $image_width) {
			$w = true;
		}
		if ($height && $height > $image_height) {
			$h = true;
		}

		return ($w || $h) ? true : false;
	}


	/**
	 * public function showCached (string $file, [string $transform], [bool $header])
	 *
	 * Print the cached image file
	 *
	 * return boolean
	 */
	public function showCached ($file, $transform = '', $header = true) {
		if (!$this->settings['cache']) {
			return false;
		}

		$Cache = new \Cache;

		if (!$cache_file = $Cache->loadFile($file.$transform, $this->settings['cache'], 'images')) {
			return false;
		}

		if ($header) {
			$info = @getImageSize($cache_file);
			header('Content-Type: '.$info['mime']);
			header('Expires: '.gmdate('D, d M Y H:i:s',(time() + $this->settings['cache'])).' GMT');
		}

		readfile($cache_file);

		die();
	}
}
?>